<?php
if(!defined('MY_ROOT'))
{
throw new Exception('Access Denied');
}
function check_email($email)
{
if (preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',
$email))
{
return true;
}else{
return false;
}
}
function showmessage($type='info',$content,$ajax=false,$return)
{
global $_G;
$smarty=$_G['hwnd']['smarty'];
$tpl=$ajax ? 'ajax_msg.tpl' : 'msg.tpl';
$tpl='common/'.$tpl;
if(!$ajax)
{
if($smarty->isCached($tpl,$type))
{
$smarty->assign('content',$content,true);
$smarty->assign('return',$return,true);
$smarty->display($tpl,$type);
return 1;
}
}
$smarty->assign('type',$type);
/*switch($type)
{
case 'success':
$smarty->assign('imageurl','static/images/common/right.jpg');
break;
case 'error':
$smarty->assign('imageurl','static/images/common/error.jpg');
break;
case 'alert':
$smarty->assign('imageurl','static/images/common/alert.jpg');
break;
case info:
default:
$smarty->assign('imageurl','static/images/common/info.jpg');
}*/
$smarty->assign('content',$content,true);
$smarty->assign('return',$return,true);
$smarty->display($tpl,$type);
}
function settings($set)
{
$r=array();
foreach($set as $setting)
{
$r[$setting['name']]=$setting['value'];
}
return $r;
}
function js2PhpTime($jsdate){
  if(preg_match('@(\d+)/(\d+)/(\d+)\s+(\d+):(\d+)@', $jsdate, $matches)==1){
    $ret = mktime($matches[4], $matches[5], 0, $matches[1], $matches[2], $matches[3]);
    //echo $matches[4] ."-". $matches[5] ."-". 0  ."-". $matches[1] ."-". $matches[2] ."-". $matches[3];
  }else if(preg_match('@(\d+)/(\d+)/(\d+)@', $jsdate, $matches)==1){
    $ret = mktime(0, 0, 0, $matches[1], $matches[2], $matches[3]);
    //echo 0 ."-". 0 ."-". 0 ."-". $matches[1] ."-". $matches[2] ."-". $matches[3];
  }
  return $ret;
}

function php2JsTime($phpDate){
    //echo $phpDate;
    //return "/Date(" . $phpDate*1000 . ")/";
    return date("m/d/Y H:i", $phpDate);
}

function php2MySqlTime($phpDate){
    return date("Y-m-d H:i:s", $phpDate);
}

function mySql2PhpTime($sqlDate){
    $arr = date_parse($sqlDate);
    return mktime($arr["hour"],$arr["minute"],$arr["second"],$arr["month"],$arr["day"],$arr["year"]);

}
function logerr($error,$type)
{
	$file=MY_ROOT.'/data/log/'.$type.'_'.'error.log';
	$error=date(DATE_RFC822,time()).'    '.$error;
	file_put_contents($file,$error,FILE_APPEND);
}
function num2cn($num,$mode=1,$lower=true,$conv=0)
{
	 $num_char = array('0','一','二','三','四','五','六','七','八','九','零','壹','贰','叁','肆','伍','陆','柒','捌','玖');
   $num_unit = array('','十','百','千','','万','亿','兆','','拾','佰','仟','','萬','億','兆');
   $date_char = array('年','月','日');
   $decimal_point = array('点','點');
   if ($lower)
    $lo_flag = 0;
   else
    $lo_flag = 1;
  
   $result = '';
   switch($mode) {
    case 1:
     preg_match_all("/^0*(\d*)\.?(\d*)/",$num,$numbers);
     break;
    case 2:
     preg_match_all("/(\d*)\.?(\d*)/",$num,$numbers);
     break;
    case 3:
     preg_match_all("/^(\d*)\-(\d*)\-(\d*)$/",$num,$numbers);
     break;
    default:
     return null;
   }
   if (isset($numbers[3][0]) && !empty($numbers[3][0])) {
    // 日期格式
    return num_to_gb($numbers[1][0],1,$lower,1) . $date_char[0] . num_to_gb($numbers[2][0]) . $date_char[1] . num_to_gb($numbers[3][0]) . $date_char[2];
   }
   if (isset($numbers[2][0]) && !empty($numbers[2][0])) {
    // 小数
    $point = $decimal_point[ count($decimal_point)/2 * $lo_flag ];
    return num_to_gb($numbers[1][0],1,$lower) . $point . num_to_gb($numbers[2][0],2,$lower,1);
   }
   if (isset($numbers[1][0]) && !empty($numbers[1][0])) {
    // 整数
    $cur_num = $numbers[1][0];
    $st_char = count($num_char)/2 * $lo_flag;
    $st_unit = count($num_unit)/2 * $lo_flag;
    $out = array();
    switch ($conv) {
     // 日期方式
     case 1:
      for($i=0;$i<strlen($cur_num);$i++)
       $out[$i] = $num_char[ $st_char+$cur_num[$i] ];
      break;
     default:
      $cur_num_s = strrev($cur_num);
      for($i=0;$i < strlen($cur_num_s);$i++) {
       if ($i%4==1 && $cur_num>=10 && $cur_num<20) //去掉 '一'十一中的'一'
        $out[$i] = '';
       else
        $out[$i] = $num_char[ $st_char+$cur_num_s[$i] ];
       $out[$i] .= $cur_num_s[$i] != '0' ? $num_unit[ $st_unit+$i%4 ] : '';
       $pre_num = $i>0 ? $cur_num_s[$i-1] : 0;
       if($cur_num_s[$i]+$pre_num == 0)
        $out[$i] = '';
       if($i%4 == 0)
        $out[$i] .= $num_unit[ $st_unit+4+floor($i/4) ];
      }
      $out = array_reverse($out);
    }
    return join('',$out);
   }
   return null;
}
?>
